import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1

import MeeGoSettings 1.0

Page {
    tools: commonTools

    GConfItem {
        id: configUsername
        key: "/apps/RosYama/username"
        defaultValue: ""
    }
    GConfItem {
        id: configPassword
        key: "/apps/RosYama/password"
        defaultValue: ""
    }
    GConfItem {
        id: configServerId
        key: "/apps/RosYama/server"
        defaultValue: 0
    }

    SelectionDialog {
        id: serverUsed
        titleText: qsTr("Select the desired server")

        model: ListModel {}
        Component.onCompleted: {
            model.append({ name: qsTr("RosYama (Debug server)"), httpUrl: "http://st1234.greensight.ru/", xmlUrl: "http://xml-st1234.greensight.ru/" })
            model.append({ name: qsTr("RosYama"), httpUrl: "http://www.rosyama.ru/", xmlUrl: "http://xml.rosyama.ru/" })
            model.append({ name: qsTr("UkrYama"), httpUrl: "http://www.ukryama.com/", xmlUrl: "http://xml.ukryama.com/" })

            selectedIndex = configServerId.value
        }
    }

    Column {
        spacing: 10
        anchors {
            left: parent.left
            right: parent.right
            verticalCenter: parent.verticalCenter
            margins: UiConstants.DefaultMargin * 5
        }

        Image {
            source: "../../images/RosYama-logo.png"
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Button {
            anchors {
                left: parent.left
                right: parent.right
            }
            text: serverUsed.model.get(serverUsed.selectedIndex).name
            onClicked: serverUsed.open()
        }

        TextField {
            id: loginField
            anchors { left: parent.left; right: parent.right; }
            placeholderText: qsTr("Login")
            inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
            text: configUsername.value
        }

        TextField {
            id: passwordField
            anchors { left: parent.left; right: parent.right; }
            placeholderText: qsTr("Password")
            echoMode: TextInput.Password
            text: configPassword.value
        }

        Button{
            id: submitButton
            text: qsTr("Enter")
            anchors { left: parent.left; right: parent.right; }

            onClicked: {
                loginField.enabled = passwordField.enabled = submitButton.enabled = false

                loginProcessing.running = true
                loginProcessing.visible = true
                rosYama.setServer(serverUsed.model.get(serverUsed.selectedIndex).xmlUrl, serverUsed.model.get(serverUsed.selectedIndex).httpUrl)
                rosYama.authorize(loginField.text, passwordField.text)
            }
        }

        Button {
            text: qsTr("Register")
            anchors {
                left: parent.left
                right: parent.right
            }
            onClicked: Qt.openUrlExternally(serverUsed.model.get(serverUsed.selectedIndex).httpUrl + "/personal/holes.php?register=yes")
        }

        BusyIndicator {
            id: loginProcessing
            visible: false
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }

    Connections {
        target: rosYama
        onLoginResult: {
            if (loginOk) {
                configUsername.value = loginField.text
                configPassword.value = passwordField.text
                configServerId.value = serverUsed.selectedIndex

                loginProcessing.visible = false
                appWindow.pageStack.replace(Qt.resolvedUrl("MainPage.qml"))
            } else {
                loginField.enabled = passwordField.enabled = submitButton.enabled = true
                loginProcessing.visible = false
                loginProcessing.running = false
            }
        }
    }

}
